/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.LiquidacionDAO;
import org.trandio.dao.LiquidacionInterface;
import org.trandio.model.DetalleFacturaVO;
import org.trandio.model.FacturaVO;

/**
 *
 * @author root
 */
public class ImprimirFactura extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    LiquidacionInterface liquidacionInterface;
    FacturaVO facturaVO;
    private List<DetalleFacturaVO> listDetalleFactura;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            liquidacionInterface = new LiquidacionDAO();
            String idfactura = request.getParameter("idfact");
            String descprestador = "";
            String descentidad = "";
            String numfactura = "FDV" + idfactura;
            String fecha = "";
            String paciente = "";
            String medico = "";
            String vrpagar = "";
            String efectivo = "";
            String descuento = "0.0";
            try {
                //traer datos de la factura
                facturaVO = liquidacionInterface.consultarFactura(idfactura);
            } catch (SQLException ex) {
                Logger.getLogger(ImprimirFactura.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NamingException ex) {
                Logger.getLogger(ImprimirFactura.class.getName()).log(Level.SEVERE, null, ex);
            }

            descprestador = facturaVO.getDescprestador();
            descentidad = facturaVO.getDescentidad();
            fecha = facturaVO.getFecha();
            paciente = facturaVO.getPaciente();
            String nit = facturaVO.getNitips();
            String codips = facturaVO.getCodips();
            int idpaciente = facturaVO.getIdpaciente();
            String mupio = facturaVO.getMupio();
            String depto = facturaVO.getDepto();


            out.println("<html>");
            out.println("<head>");
            out.println("<title>Factura No. " + facturaVO.getNumfactura() + "</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<div onclick='window.print()' style='float:right; border: 1px solid; cursor:pointer'>");
            out.println("<img src='../common/img/imprimir.gif' width='24px' heigth='24px' />");
            out.println("</div>");
            //tabla del encabezado
            out.println("<table align='center' style='font-size : 9px;' width='100%'>");
            out.println("<tr>");
            out.println("<td colspan='2' align='center'>" + descprestador + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td colspan='2' align='center'>NIT: " + nit + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td colspan='2' align='center'>" + mupio + " - " + depto + "</td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<table style='font-size:9px;' align='center' width='100%'>");
            out.println("<tr>");
            out.println("<td>Aseguradora</td>");
            out.println("<td colspan='3'>" + descentidad + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>No. Factura</td>");
            out.println("<td>" + numfactura + "</td>");
            out.println("<td>Fecha</td>");
            out.println("<td>" + fecha + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Paciente</td>");
            out.println("<td>" + paciente + "</td>");
            out.println("</tr>");
            out.println("<tr>");
            out.println("<td>Medico</td>");
            out.println("<td>" + medico + "</td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("<table cellpadding='0' cellspacing='0' style='font-size:9px;' align='center' width='100%'>");
            out.println("<tr>");
            out.println("<th style='border:1px solid;'>Codigo</th>");
            out.println("<th style='border:1px solid;'>Descripcion</th>");
            out.println("<th style='border:1px solid;'>Tarifa</th>");
            out.println("<th style='border:1px solid;'>Vr Unitario</th>");
            out.println("<th style='border:1px solid;'>Cantidad</th>");
            out.println("<th style='border:1px solid;'>Vr Paciente</th>");
            out.println("<th style='border:1px solid;'>Vr Aseguradora</th>");
            out.println("<th style='border:1px solid;'>Vr Total</th>");
            out.println("</tr>");
            try {
                listDetalleFactura = liquidacionInterface.consultarDetallesFactura(idfactura);
            } catch (SQLException ex) {
                Logger.getLogger(ImprimirFactura.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NamingException ex) {
                Logger.getLogger(ImprimirFactura.class.getName()).log(Level.SEVERE, null, ex);
            }
            Iterator it = listDetalleFactura.iterator();
            float totalPaciente = 0;
            float totalAseguradora = 0;
            float totalFactura = 0;

            while (it.hasNext()) {
                DetalleFacturaVO b = (DetalleFacturaVO) it.next();

                out.println("<tr>");
                out.println("<td>" + b.getCodigo() + "</td>");
                out.println("<td>" + b.getDescripcion() + "</td>");
                out.println("<td align='right'>" + b.getTarifa() + "</td>");
                out.println("<td align='right'>" + b.getVrunitario() + "</td>");
                out.println("<td align='center'>" + b.getCantidad() + "</td>");
                out.println("<td align='right'>" + b.getVrpaciente() + "</td>");
                out.println("<td align='right'>" + b.getVraseguradora() + "</td>");
                out.println("<td align='right'>" + b.getVrtotal() + "</td>");
                out.println("<tr>");

                totalPaciente = totalPaciente + b.getVrpaciente();
                totalAseguradora = totalAseguradora + b.getVraseguradora();
                totalFactura = totalFactura + b.getVrtotal();
            }

            out.println("<tr>");
            out.println("<td></td>");
            out.println("<td></td>");
            out.println("<td align='right'></td>");
            out.println("<td align='right'></td>");
            out.println("<td align='center'><b>Totales</b></td>");
            out.println("<td style='border:1px solid;' align='right'><b>" + totalPaciente + "</b></td>");
            out.println("<td style='border:1px solid;' align='right'><b>" + totalAseguradora + "</b></td>");
            out.println("<td style='border:1px solid;' align='right'><b>" + totalFactura + "</b></td>");
            out.println("<tr>");
            out.println("</table>");
            out.println("<form>");
            out.println("<input type=button value='Cerrar' onclick='window.close()'>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");


        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
